var addPage = avalon.define({
	$id: "page",
	uid: "",
	list:{
		couponImage:''
	},
	totalScore:0,
	quantity:1,
	dt:''
});

mui.ready(function() {
	var uid = $("#uid").val();
	addPage.uid = uid;	
	mui('#form').on('tap','#showAgentPicker',function(e){
		agentPicker.show(function(items){
			$('#showAgentPicker').text(items[0].text);
			$('#dt').val(items[0].value);
			addPage.dt=items[0].value;
			// 返回false可以阻止选择框的关闭
			// return false;
		});
	});
	mui('#form').on('change', '#quantity', function(e){
		addPage.quantity=$('#quantity').val();
		addPage.totalScore=addPage.list.score*addPage.quantity;
	});
	var _id = $.getParameter("id");
	mui.ajax('/bonus/market/get.json', {
		data: {
			id: _id
		},
		dataType: 'json',
		success: function(data) {			
			if(""!=data&&null!=data){
				addPage.list=data;
				addPage.totalScore=data.score;
			}
			var nbox = mui("#quantityBox").numbox();
			nbox.options.max=data.quantity;
			console.log(data, addPage.list, nbox);
		}
	});
	var agentPicker=new mui.PopPicker();
	mui.ajax('/customer/dt.json', {
		data: {
			id: uid
		},
		type: 'post',
		dataType: 'json',
		success: function(data) {
			if('1'==data.status) {
				var _dt = [],_text='';
				data.object.distributor.forEach(function(item){
					_dt.push({
						value:item.id,
						text:item.companyName
					});
					if(item.id==data.object.dt) {
						_text=item.companyName;
					}
				});
				agentPicker.setData(_dt);
				if(_text!='') {
					agentPicker.pickers[0].setSelectedValue(data.object.dt);
					$('#showAgentPicker').text(_text);
					$('#dt').val(data.object.dt);
					addPage.dt=data.object.dt;
				}
			}
		}
	});
	mui('.my-footer').on('tap','.my-btn-ok',function(){
		var _param={};
		_param.score=addPage.list.score;
		_param.quantity=addPage.quantity;
		_param.value=addPage.list.value;
		_param['user.id']=uid;
		_param['user.customer.id']=uid;
		_param['regionLocation.id']=addPage.list.region.id;
		// addPage.list.countryLocation && _param['countryLocation.id']=addPage.list.countryLocation.id;
		_param['coupon.id']=addPage.list.id;
		_param['dt.id']=addPage.dt;
		_param['currency.id']=addPage.list.currency.id;
		_param.coupontype=addPage.list.couponType || '';
		_param.starttime=$.getCurrDate();
		_param.endtime=$.getDate(_param.starttime, addPage.list.validityDay);
		_param.rule=addPage.list.rule;
		console.log(addPage, _param);
		if(formValid(_param)) {
			return;
		}
		$.openLoadForm($.i18n.prop('contentRefresh'));
		mui.ajax('/customer/coupon/exchange.json', {
			data: _param,
			type: 'post',
			dataType: 'json',
			success: function(data) {
				console.log(data);
				$.closeLoadForm();
				if(data.status==1) {
					mui.alert($.i18n.prop('Coupon_message1'), "", $.i18n.prop('User_Center_ok'), function(e) {
						// var url = "../market/index.html";
						// mui.openWindow({
						// 	url: url
						// });
						mui.back();
					});
				}else if(data.status==2){
					mui.alert($.i18n.prop('Coupon_message2'));
				}else if(data.status==3){
					mui.alert($.i18n.prop('Coupon_message3'));
				}
			}
		});

	});

	function formValid(obj){
		if(obj['dt.id']=="") {
			mui.alert($.i18n.prop('Coupon_message4'));
			return true;
		}
		return false;
	}
});
